<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="项目ID" prop="projectId">
        <el-input v-model="formData.projectId" placeholder="请输入项目ID" />
      </el-form-item>
      <el-form-item label="雇主ID" prop="employerId">
        <el-input v-model="formData.employerId" placeholder="请输入雇主ID" />
      </el-form-item>
      <el-form-item label="人才ID" prop="wordkId">
        <el-input v-model="formData.wordkId" placeholder="请输入人才ID" />
      </el-form-item>
      <el-form-item label="状态：1-待发送 2-已发送 3-已接受 4-已拒绝 5-已过期 6-已撤销" prop="offerStatus">
        <el-input v-model="formData.offerStatus" placeholder="请输入状态：1-待发送 2-已发送 3-已接受 4-已拒绝 5-已过期 6-已撤销" />
      </el-form-item>
      <el-form-item label="Offer过期时间" prop="expireTime">
        <el-date-picker
          v-model="formData.expireTime"
          type="date"
          value-format="x"
          placeholder="选择Offer过期时间"
        />
      </el-form-item>
      <el-form-item label="接受时间" prop="acceptTime">
        <el-date-picker
          v-model="formData.acceptTime"
          type="date"
          value-format="x"
          placeholder="选择接受时间"
        />
      </el-form-item>
      <el-form-item label="拒绝时间" prop="rejectTime">
        <el-date-picker
          v-model="formData.rejectTime"
          type="date"
          value-format="x"
          placeholder="选择拒绝时间"
        />
      </el-form-item>
      <el-form-item label="拒绝原因" prop="rejectReason">
        <el-input v-model="formData.rejectReason" placeholder="请输入拒绝原因" />
      </el-form-item>
      <el-form-item label="撤销时间" prop="cancelTime">
        <el-date-picker
          v-model="formData.cancelTime"
          type="date"
          value-format="x"
          placeholder="选择撤销时间"
        />
      </el-form-item>
      <el-form-item label="撤销原因" prop="cancelReason">
        <el-input v-model="formData.cancelReason" placeholder="请输入撤销原因" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ProjectOfferApi, ProjectOfferVO } from '@/api/xinyu/projectoffer'

/** Offer主 表单 */
defineOptions({ name: 'ProjectOfferForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  projectId: undefined,
  employerId: undefined,
  wordkId: undefined,
  offerStatus: undefined,
  expireTime: undefined,
  acceptTime: undefined,
  rejectTime: undefined,
  rejectReason: undefined,
  cancelTime: undefined,
  cancelReason: undefined,
})
const formRules = reactive({
  projectId: [{ required: true, message: '项目ID不能为空', trigger: 'blur' }],
  employerId: [{ required: true, message: '雇主ID不能为空', trigger: 'blur' }],
  wordkId: [{ required: true, message: '人才ID不能为空', trigger: 'blur' }],
  offerStatus: [{ required: true, message: '状态：1-待发送 2-已发送 3-已接受 4-已拒绝 5-已过期 6-已撤销不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProjectOfferApi.getProjectOffer(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProjectOfferVO
    if (formType.value === 'create') {
      await ProjectOfferApi.createProjectOffer(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProjectOfferApi.updateProjectOffer(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    projectId: undefined,
    employerId: undefined,
    wordkId: undefined,
    offerStatus: undefined,
    expireTime: undefined,
    acceptTime: undefined,
    rejectTime: undefined,
    rejectReason: undefined,
    cancelTime: undefined,
    cancelReason: undefined,
  }
  formRef.value?.resetFields()
}
</script>